import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import axios from 'axios'
import { noticeUrl } from '@/utils/url.js'
const state = {
    list: []
}
const mutations = {
    setList(state, data) {
        state.list = data
    }
}
const actions = {
    get({ commit }, getdata) {
        return new Promise((resolve, reject) => {
            axios({
                url: noticeUrl,
                method: 'get',
                params: {
                    transform: 1,
                    order: "id,desc",
                    filter: `title,cs,${getdata.search}`,
                    page: `${getdata.curP},${getdata.pageSize}`
                }
            }).then(res => {
                commit('setList', res.data.notice)
                resolve(res)
            })
        })
    },
    save({ commit }, form) {
        return new Promise((resolve, reject) => {
            let method = 'post',
                url = noticeUrl
            if (form.id) {
                method = 'put',
                    url = noticeUrl + "/" + form.id
            }
            axios({
                url,
                method,
                data: form
            }).then(res => {
                resolve()
            })
        })
    },
    del({ dispatch }, ids) {
        return new Promise((resolve, reject) => {
            axios({
                url: (noticeUrl + "/" + ids),
                method: 'delete'
            }).then(res => {
                dispatch('getData')
                resolve()
            })
        })
    }
}
const getters = {}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
